package com.demo.androidgame.samgukji_hit;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class ActOneScreen extends GLScreen {

	// 히어로 <-> 뮤직 상수
	private final int HEROGAME = 1;
	private final int MUSICGAME = 2;

	int gameState;

	Camera2D guiCam;
	SpriteBatcher batcher;

	Rectangle mainMenu;
	Rectangle saveMenu;
	Rectangle upgradeMenu;
	Rectangle startMenu;
	Rectangle actMenu;
	Rectangle changeMenu;
	Vector2 touchPoint;

	Rectangle[] heroAreas, musicAreas;

	public ActOneScreen(Game game) {
		super(game);
		guiCam = new Camera2D(glGraphics, 800, 480);
		batcher = new SpriteBatcher(glGraphics, 100);

		// 충돌영역--------------------------------------------------------------------------------------------------------------------

		// 메인 메뉴
		mainMenu = new Rectangle(105, 11, 110, 38); // 메인에뉴 버튼
		saveMenu = new Rectangle(105 + 140, 11, 110, 38); // 세이브 버튼
		upgradeMenu = new Rectangle(105 + 280, 11, 110, 38); // 업그레이드 버튼
		startMenu = new Rectangle(105 + 420, 11, 110, 38); // 스타트 버튼
		actMenu = new Rectangle(105 + 560, 11, 110, 38); // 엑트 버튼
		changeMenu = new Rectangle(0, 0, 80, 80); // 히어로 <-> 뮤직 전환 버튼
		
		heroAreas = new Rectangle[6];
		musicAreas = new Rectangle[6];

		//히어로 스테이지 
		heroAreas[0] = new Rectangle(0, 206, 80, 37);
		heroAreas[1] = new Rectangle(175, 181, 80, 37);
		heroAreas[2] = new Rectangle(360, 241, 80, 37);
		heroAreas[3] = new Rectangle(185, 281, 80, 37);
		heroAreas[4] = new Rectangle(70, 371, 80, 37);
		heroAreas[5] = new Rectangle(310, 371, 80, 37);
		// 뮤직 스테이지
		musicAreas[0] = new Rectangle(0, 206, 80, 37);
		musicAreas[1] = new Rectangle(175, 181, 80, 37);
		musicAreas[2] = new Rectangle(360, 241, 80, 37);
		musicAreas[3] = new Rectangle(185, 281, 80, 37);
		musicAreas[4] = new Rectangle(70, 371, 80, 37);
		musicAreas[5] = new Rectangle(310, 371, 80, 37);
		
		touchPoint = new Vector2();
		// -------------------------------------------------------------------------------------------------------------------------------

		gameState = HEROGAME;
	}

	@Override
	public void update(float deltaTime) {

		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				touchPoint.set(event.x, event.y);
				guiCam.touchToWorld(touchPoint);

				if (OverlapTester.pointInRectangle(mainMenu, touchPoint)) {
					game.setScreen(new MainScreen(game));
					return;
				}
				if (OverlapTester.pointInRectangle(saveMenu, touchPoint)) {

					return;
				}
				if (OverlapTester.pointInRectangle(upgradeMenu, touchPoint)) {

					return;
				}
				if (OverlapTester.pointInRectangle(startMenu, touchPoint)) {

					return;
				}
				if (OverlapTester.pointInRectangle(actMenu, touchPoint)) {
					game.setScreen(new ActScreen(game));
					return;
				}
				if (OverlapTester.pointInRectangle(changeMenu, touchPoint)) {

					if (gameState == HEROGAME) {
						gameState = MUSICGAME;
					} else {
						gameState = HEROGAME;
					}

					return;
				}
				// 엑트 1----------------------------------------------------------------------------------------------------------
				if (Assets.actNumber >= 1) {
					// 히어로 스테이지
					if (gameState == HEROGAME) {
						if (Assets.clearHeroStage >= 111) {
							if (OverlapTester.pointInRectangle(heroAreas[0], touchPoint)) {
								game.setScreen(new HeroStageScreen(game));
								if (Assets.clearHeroStage < 112) {
									Assets.clearHeroStage = 112;
								}
								return;
							}
						}
						if (Assets.clearHeroStage >= 112
								&& Assets.clearMusicStage >= 212) {
							if (OverlapTester.pointInRectangle(heroAreas[1],
									touchPoint)) {
								game.setScreen(new MainScreen(game));
								if (Assets.clearHeroStage < 113) {
									Assets.clearHeroStage = 113;
								}
								return;
							}
						}
						if (Assets.clearHeroStage >= 113
								&& Assets.clearMusicStage >= 213) {
							if (OverlapTester.pointInRectangle(heroAreas[2],
									touchPoint)) {
								game.setScreen(new MainScreen(game));
								if (Assets.clearHeroStage < 114) {
									Assets.clearHeroStage = 114;
								}
								return;
							}
						}
						if (Assets.clearHeroStage >= 114
								&& Assets.clearMusicStage >= 214) {
							if (OverlapTester.pointInRectangle(heroAreas[3],
									touchPoint)) {
								game.setScreen(new MainScreen(game));
								if (Assets.clearHeroStage < 115) {
									Assets.clearHeroStage = 115;
								}
								return;
							}
						}
						if (Assets.clearHeroStage >= 115
								&& Assets.clearMusicStage >= 215) {
							if (OverlapTester.pointInRectangle(heroAreas[4],
									touchPoint)) {
								game.setScreen(new MainScreen(game));
								if (Assets.clearHeroStage < 116) {
									Assets.clearHeroStage = 116;
								}
								return;
							}
						}
						if (Assets.clearHeroStage >= 116
								&& Assets.clearMusicStage >= 216) {
							if (OverlapTester.pointInRectangle(heroAreas[5],
									touchPoint)) {
								game.setScreen(new MainScreen(game));
								if (Assets.clearHeroStage < 121) {
									Assets.clearHeroStage = 121;
								}
								return;
							}
						}
					} else if (gameState == MUSICGAME) {
						// 뮤직 스테이지
						if (Assets.clearMusicStage >= 211
								&& Assets.clearHeroStage >= 112) {
							if (OverlapTester.pointInRectangle(musicAreas[0],
									touchPoint)) {
								game.setScreen(new musicStage1_1(game));
								if (Assets.clearMusicStage < 212) {
									Assets.clearMusicStage = 212;
								}
								return;
							}
						}
						if (Assets.clearMusicStage >= 212
								&& Assets.clearHeroStage >= 113) {
							if (OverlapTester.pointInRectangle(musicAreas[1],
									touchPoint)) {
								game.setScreen(new MainScreen(game));
								if (Assets.clearMusicStage < 213) {
									Assets.clearMusicStage = 213;
								}
								return;
							}
						}
						if (Assets.clearMusicStage >= 213
								&& Assets.clearHeroStage >= 114) {
							if (OverlapTester.pointInRectangle(musicAreas[2],
									touchPoint)) {
								game.setScreen(new MainScreen(game));
								if (Assets.clearMusicStage < 214) {
									Assets.clearMusicStage = 214;
								}
								return;
							}
						}
						if (Assets.clearMusicStage >= 214
								&& Assets.clearHeroStage >= 115) {
							if (OverlapTester.pointInRectangle(musicAreas[3],
									touchPoint)) {
								game.setScreen(new MainScreen(game));
								if (Assets.clearMusicStage < 215) {
									Assets.clearMusicStage = 215;
								}
								return;
							}
						}
						if (Assets.clearMusicStage >= 215
								&& Assets.clearHeroStage >= 116) {
							if (OverlapTester.pointInRectangle(musicAreas[4],
									touchPoint)) {
								game.setScreen(new MainScreen(game));
								if (Assets.clearMusicStage < 216) {
									Assets.clearMusicStage = 216;
								}
								return;
							}
						}
						if (Assets.clearMusicStage >= 216
								&& Assets.clearHeroStage >= 121) {
							if (OverlapTester.pointInRectangle(musicAreas[5],
									touchPoint)) {
								game.setScreen(new MainScreen(game));
								if (Assets.clearMusicStage < 221) {
									Assets.clearMusicStage = 221;
								}
								return;
							}
						}
					}
				}
			}
		}
	}

	@Override
	public void present(float deltaTime) {
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		guiCam.setViewportAndMatrices();

		gl.glEnable(GL10.GL_TEXTURE_2D);

		// 로드게임 배경
		batcher.beginBatch(Assets.mLoadBackground);
		batcher.drawSprite(400, 280, 800, 400, Assets.mLoadBackRegion);
		batcher.endBatch();

		// 로드게임 메뉴
		batcher.beginBatch(Assets.mLoadMenu);
		batcher.drawSprite(160, 30, 110, 38, Assets.mMainMenuRegion);
		batcher.drawSprite(160 + 140, 30, 110, 38, Assets.mSaveMenuRegion);
		batcher.drawSprite(160 + 280, 30, 110, 38, Assets.mUpgradeMenuRegion);
		batcher.drawSprite(160 + 420, 30, 110, 38, Assets.mStartMenuRegion);
		batcher.drawSprite(160 + 560, 30, 110, 38, Assets.mMainMenuRegion);
		batcher.endBatch();

		// 히어로 <-> 뮤직 스테이지 전환
		batcher.beginBatch(Assets.mChangeMenu);
		if (gameState == HEROGAME) {
			batcher.drawSprite(40, 30, 92, 92, Assets.mHeroMenuRegion);
		} else if (gameState == MUSICGAME) {
			batcher.drawSprite(40, 30, 92, 92, Assets.mMusicMenuRegion);
		}
		batcher.endBatch();

		// 히어로 스테이지
		if (gameState == HEROGAME) {
			batcher.beginBatch(Assets.mStageMenu);
			batcher.drawSprite(40, 225, 80, 37, Assets.mStageMenuRegion);
			batcher.drawSprite(215, 200, 80, 37, Assets.mStageMenuRegion);
			batcher.drawSprite(400, 260, 80, 37, Assets.mStageMenuRegion);
			batcher.drawSprite(225, 300, 80, 37, Assets.mStageMenuRegion);
			batcher.drawSprite(110, 390, 80, 37, Assets.mStageMenuRegion);
			batcher.drawSprite(350, 390, 80, 37, Assets.mStageMenuRegion);
			batcher.endBatch();
		}
		// 뮤직 스테이지
		else if (gameState == MUSICGAME) {
			batcher.beginBatch(Assets.mMusicStageMenu);
			batcher.drawSprite(40, 225, 80, 37, Assets.mMusicStageMenuRegion);
			batcher.drawSprite(215, 200, 80, 37, Assets.mMusicStageMenuRegion);
			batcher.drawSprite(400, 260, 80, 37, Assets.mMusicStageMenuRegion);
			batcher.drawSprite(225, 300, 80, 37, Assets.mMusicStageMenuRegion);
			batcher.drawSprite(110, 390, 80, 37, Assets.mMusicStageMenuRegion);
			batcher.drawSprite(350, 390, 80, 37, Assets.mMusicStageMenuRegion);
			batcher.endBatch();
		}
		gl.glEnable(GL10.GL_BLEND);
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

	}

	@Override
	public void pause() {

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
